<?php

namespace Biz\service\advertisement\apiModule;

use Biz\model\advertisement\MPositionBaseInfo;
use Biz\service\advertisement\AdUserProfileManagement;
use Doraemon\cache\UserNoter;
use Doraemon\service\Totem\TotemService;

class HomePopups extends TabPopups
{

    public function extCheckActive($v, $extInput)
    {
        if ($v['id'] == 59567) {
            return !\Doraemon\model\AppContentSync::getContentSync(['uid' => $this->userProfile->uid]);
        }

        return true;
    }

    public function staticList($uid, $userProfileExt): array
    {

        $banner = [];
        $userProfile = AdUserProfileManagement::getUserProfile($uid, $userProfileExt, ['birthday', 'addtime']);

        // 如果当天是生日，则展示生日弹屏 如果是数字要把数字转成时间戳不能把19930404当做时间戳
//        $birthday = date('m-d', is_numeric($userProfile->birthday) ? $userProfile->birthday : strtotime($userProfile->birthday));
        $birthday = date('m-d', strtotime((string)$userProfile->birthday));
        $now_date = date('m-d', time());
        if ($userProfile->birthday && $birthday == $now_date) {
            $banner[] = 'b14cc30kv0pu20000p72how';
        } else {
            if ($anniversary = $this->inAnniversary($uid, $userProfile->addtime)) $banner[] = $anniversary;
        }

        $static = [];
        $date = date("Y-m-d");
        foreach ($banner as $pic) {
            $static[] = [
                'id' => '-1',
                'position_id' => '64',
                'title' => '',
                'remark' => '',
                'is_share' => MPositionBaseInfo::IS_NOT_SHARE,
                'sort' => '103',
                'info_status' => '1',
                'start_show_time' => $date . ' 00:00:00',
                'end_show_time' => $date . ' 23:59:59',
                'admin_id' => '23',
                'addtime' => '2021-12-09 18:40:14',
                'edittime' => '1970-01-02 13:00:00',
                'pos' => '0',
                'single_pic_id' => '',
                'pic_id' => '',
                'link_type' => '',
                'link_params' => '[]',
                'link_url' => 'hhz://',
                'out_link' => '',
                'ext_type' => '0',
                'reason' => '3',
                'client_uid' => '0',
                'client_nick' => '',
                'show_type' => '2',
                'close_type' => '9',
                'rel_id' => '0',
                'ext' => [
                    'id' => '92',
                    'popups_type' => '3',
                    'title' => '',
                    'scene_type' => '1',
                    'bg_conf' => '{"close":0,"bgPic":"' . $pic . '","bgLink":{"link_type":"hhz:\\/\\/"}}',
                    'img_conf' => '{"pos":0,"pic":"","height":"","link":[],"text":"","img_w":"","img_h":""}',
                    'bt_conf' => '{"btBgColor1":"","btBgColor2":"","btTitle1":"","btTitle2":"","btColor1":"","btColor2":"","btLink1":[],"btLink2":[],"scenesType1":"","scenesType2":""}',
                    'popups_status' => '1',
                    'addtime' => '2021-12-09 18:40:14',
                    'edittime' => '0000-00-00 00:00:00',
                    'relate_id' => '-1',
                ],
                '_sort' => [
                    0 => '0',
                    1 => '7',
                    2 => '0',
                    3 => '-1',
                ]
            ];
        }

        $totem = TotemService::isPopupTabTotem($uid);
        if ($totem) {
            $static[] = [
                'id' => '-2',
                'position_id' => '64',
                'title' => '',
                'remark' => '',
                'is_share' => MPositionBaseInfo::IS_NOT_SHARE,
                'sort' => '103',
                'info_status' => '1',
                'start_show_time' => $date . ' 00:00:00',
                'end_show_time' => $date . ' 23:59:59',
                'admin_id' => '23',
                'addtime' => '2021-12-09 18:40:14',
                'edittime' => '1970-01-02 13:00:00',
                'pos' => '0',
                'single_pic_id' => '',
                'pic_id' => '',
                'link_type' => '',
                'link_params' => '[]',
                'link_url' => 'hhz://',
                'out_link' => '',
                'ext_type' => '0',
                'reason' => '3',
                'client_uid' => '0',
                'client_nick' => '',
                'show_type' => '2',
                'close_type' => '9',
                'rel_id' => '0',
                'ext' => [
                    'id' => '92',
                    'popups_type' => '5',
                ],
                '_data' => $totem,
                '_sort' => [
                    0 => '0',
                    1 => '7',
                    2 => '0',
                    3 => '-2',
                ]
            ];
        }

        return $static;
    }


    public static function inAnniversary($uid, $addtime)
    {
        $now = date('Ymd');
        $earlier = new \DateTime(date('Ymd', $addtime));
        $later = new \DateTime($now);
        $anniversary = [
            1 => '970d232eu2zi00000p7b9li',
            2 => '224e830kv0pu20000p72htq',
            3 => 'fb4cc30kv0pu20000p72hv0',
        ];

        foreach ($anniversary as $i => $banner) {
            $earlier->modify("+ 1 year");
            if ($earlier->format("Ym") == $later->format("Ym")) {
                $diff = $later->diff($earlier)->format("%a");
                if ($later->format("Ymd") >= $earlier->format("Ymd") && $diff <= 7) {
                    // 如果已设置展示过，那么查看展示的日期是否为当天，如果是当天则继续展示，否则不展示
                    if (!UserNoter::getAnniversaryShowDate($uid)) {
                        UserNoter::setAnniversaryShowDate($uid, date('Ymd', time()));
                        return $banner;
                    }
                }
            }
        }
    }

}